﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PB.FBConnect;
using System.Web.Security;

namespace PB.FBMembershipWebApp.Admin
{
    public partial class ManageUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UsersRepeater.ItemDataBound +=new RepeaterItemEventHandler(UsersRepeater_ItemDataBound);
            UsersRepeater.ItemCommand +=new RepeaterCommandEventHandler(UsersRepeater_ItemCommand);
            if (!Page.IsPostBack)
            {
                List<FBUser> usersList = FBMembership.GetAllUsers();
                UsersRepeater.DataSource = usersList;
                UsersRepeater.DataBind();
            }
            
        }

        void UsersRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
        
            RepeaterItem repeaterItem = e.Item;
            FBUser fbUser = null;
            if (repeaterItem != null)
            {
                Literal ltlUid = (Literal)repeaterItem.FindControl("ltlUid");
                if (ltlUid != null)
                {
                    fbUser = (FBUser)Membership.GetUser(ltlUid.Text);
                }
                if (fbUser != null)
                {
                    CheckBox IsLockedCheckBox = (CheckBox)repeaterItem.FindControl("IsLockedCheckBox");
                    if (IsLockedCheckBox != null)
                    {
                        if (fbUser.CustomUser.IsLocked != IsLockedCheckBox.Checked)
                        {
                            fbUser.CustomUser.IsLocked = IsLockedCheckBox.Checked;
                            Membership.UpdateUser(fbUser);
                        }
                    }

                    CheckBoxList RolesCheckBoxList = (CheckBoxList)repeaterItem.FindControl("RolesCheckBoxList");
                    if (RolesCheckBoxList != null)
                    {
                        List<string> newRoles = new List<string>();
                        foreach (ListItem li in RolesCheckBoxList.Items)
                        {
                            if (li.Selected == true)
                            {
                                newRoles.Add(li.Text);
                            }
                        }
                        Roles.AddUserToRoles(fbUser.UserName, newRoles.ToArray<string>()); ;
                    }
                }
            }
        }

        void UsersRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                FBUser fbUser = (FBUser)e.Item.DataItem;
                BindControls(fbUser, e.Item);
            }
        }

        private void BindControls(FBUser fbUser, RepeaterItem repeaterItem)
        {
            
            Image profileImage = (Image)repeaterItem.FindControl("ProfileImage");
            if (profileImage != null)
            {
                
                if (string.IsNullOrEmpty(fbUser.CustomUser.Picture))
                    profileImage.Visible = false;
                else
                    profileImage.ImageUrl = fbUser.CustomUser.Picture;

                if (!string.IsNullOrEmpty(profileImage.ImageUrl) && profileImage.ImageUrl == "default")
                {
                    profileImage.ImageUrl = "http://static.ak.connect.facebook.com/pics/s_silhouette.jpg";
                }
            }



            Literal ltlUid = (Literal)repeaterItem.FindControl("ltlUid");
            if (ltlUid != null)
            {
                ltlUid.Text = fbUser.CustomUser.FacebookUserId.ToString();
            }

            Literal ltlUserName = (Literal)repeaterItem.FindControl("ltlUserName");
            if (ltlUserName != null)
            {
                ltlUserName.Text = fbUser.CustomUser.DisplayName;
            }

            //Literal ltlStartDate = (Literal)repeaterItem.FindControl("ltlStartDate");
            //if (ltlUserName != null)
            //{
            //    ltlStartDate.Text = fbUser.CustomUser.StartDate.ToString("g");
            //}

            CheckBox IsLockedCheckBox = (CheckBox)repeaterItem.FindControl("IsLockedCheckBox");
            if (IsLockedCheckBox != null)
            {
                IsLockedCheckBox.Checked = fbUser.CustomUser.IsLocked;
            }

            Button UpdateButton = (Button)repeaterItem.FindControl("UpdateButton");
            if (UpdateButton != null)
            {

            }

            CheckBoxList RolesCheckBoxList = (CheckBoxList)repeaterItem.FindControl("RolesCheckBoxList");
            if (RolesCheckBoxList != null)
            {
                RolesCheckBoxList.DataSource = Roles.GetAllRoles();
                RolesCheckBoxList.DataBind();
                string[] userRoles = Roles.GetRolesForUser(fbUser.UserName);
                foreach(string r in userRoles)
                {
                    int t = RolesCheckBoxList.Items.IndexOf(new ListItem(r));
                    if ( t >= 0)
                    {
                        RolesCheckBoxList.Items[t].Selected = true;
                    }
                }
                
            }
        }
    }
}
